home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / v8n07.arc / LINKTEXT.PAS < prev    next >
Pascal/Delphi Source File  |  1989-03-13  |  4KB  |  120 lines

  1. PROGRAM LinkText;
  2. USES Crt;     { Omit this line for version 3 }
  3. TYPE
  4.   AnyStr = STRING[255];
  5.   LinePtr = ^LineRecType;
  6.   LineRecType = RECORD
  7.                   NextLine : LinePtr;
  8.                   LineField : AnyStr;
  9.                 END;              { of LineRecType }
  10.  
  11. VAR
  12.   TextFile : Text;                { Input File     }
  13.   Head : LinePtr;                 { Head of List   }
  14.   Hold : LinePtr;                 { Place Holder   }
  15.   Cur : LinePtr;                  { Current Line   }
  16.   FileName : AnyStr;              { File Name      }
  17.   Ok : Boolean;                   { I/O Error Flag }
  18.   LineBuf : AnyStr;               { Input String   }
  19.   L : Integer;                    { Line Number    }
  20.  
  21.  
  22.   PROCEDURE LoadFile;
  23.   { LoadFile prompts for a file name and
  24.    loads the file into a linked list }
  25.   BEGIN
  26.     ClrScr;
  27.     Write('Enter File Name: ');
  28.     ReadLn(FileName);
  29.     Assign(TextFile, FileName);
  30.     {$I-} Reset(TextFile) {I+} ;
  31.     Ok := (IoResult = 0);
  32.     IF Ok THEN
  33.       BEGIN
  34.         GetMem(Head, 4);
  35.         Head^.NextLine := NIL;    { Initialize Head }
  36.         Hold := Head;
  37.         WHILE NOT Eof(TextFile) DO
  38.           BEGIN
  39.             ReadLn(TextFile, LineBuf);
  40.             GetMem(Cur, Length(LineBuf)+5); { Allocate Memory }
  41.             Hold^.NextLine := Cur; { Set previous pointer }
  42.             Cur^.NextLine := NIL; { Cur goes at end of list }
  43.             Hold := Cur;          { Save Current pointer }
  44.             Cur^.LineField := LineBuf;
  45.           END;
  46.         Close(TextFile);
  47.       END;
  48.   END;                            { of LoadFile }
  49.  
  50.   PROCEDURE DisplayFile;
  51.   { DisplayFile traverses the list and writes each line }
  52.   BEGIN
  53.     ClrScr;
  54.     Cur := Head^.NextLine;
  55.     WHILE Cur <> NIL DO
  56.       BEGIN
  57.         WriteLn(Cur^.LineField);
  58.         Cur := Cur^.NextLine;
  59.       END;
  60.   END;                            { of DisplayFile }
  61.  
  62.   PROCEDURE DeleteLine(N : Integer);
  63.   { DeleteLine deletes the Nth line of the buffer. }
  64.   VAR
  65.     Count : Integer;
  66.   BEGIN
  67.     Hold := Head;
  68.     Cur := Head^.NextLine;
  69.     Count := 1;
  70.     WHILE (Count < N) AND (Cur <> NIL) DO
  71.       BEGIN
  72.         Hold := Cur;              { Save Current pointer }
  73.         Cur := Cur^.NextLine;     { Advance to next line }
  74.         Count := Succ(Count);     { Increment counter    }
  75.       END;
  76.     IF (Cur <> NIL) AND (Count = N) THEN
  77.       BEGIN
  78.         Hold^.NextLine := Cur^.NextLine; {  skip current line }
  79.         FreeMem(Cur, Length(Cur^.LineField)+5);
  80.       END;
  81.   END;                            { of DeleteLine }
  82.  
  83.   PROCEDURE InsertLine(N : Integer; NewStr : AnyStr);
  84.   { InsertLine inserts the line NewStr before line N. }
  85.   VAR
  86.     Count : Integer;
  87.     NewLine : LinePtr;
  88.   BEGIN
  89.     Hold := Head;
  90.     Cur := Head^.NextLine;
  91.     Count := 1;
  92.     WHILE (Count < N) AND (Cur <> NIL) DO
  93.       BEGIN
  94.         Hold := Cur;              { Save current pointer }
  95.         Cur := Cur^.NextLine;     { Advance to next line }
  96.         Count := Succ(Count);     { Increment counter    }
  97.       END;
  98.     GetMem(NewLine, Length(NewStr)+5);
  99.     Hold^.NextLine := NewLine;    { Change pointers to link }
  100.     NewLine^.NextLine := Cur;     {   in the new line       }
  101.     NewLine^.LineField := NewStr;
  102.   END;                            { of InsertLine }
  103.  
  104. BEGIN                             { Main }
  105.   LoadFile;
  106.   IF Ok THEN
  107.     BEGIN
  108.       DisplayFile;
  109.       WriteLn; Write('Enter line number to delete: '); ClrEol;
  110.       ReadLn(L);
  111.       DeleteLine(L);
  112.       DisplayFile;
  113.       WriteLn; Write('Insert string before what line? '); ClrEol;
  114.       ReadLn(L);
  115.       InsertLine(L, 'This string to be inserted before line specified');
  116.       DisplayFile;
  117.     END
  118.   ELSE WriteLn(FileName, ' NOT FOUND');
  119. END.
  120.